#include <iostream>
#include <string>
#include <mysql/mysql.h>

const std::string host = "127.0.0.1";
const std::string user = "lgf";
const std::string passwd = "123456";
const std::string db = "test";
const unsigned int port = 3306;

int main()
{
    // std::cout << "client version: " << mysql_get_client_info() << std::endl;
    // MYSQL *mysql_init(nullptr);// 初始化对象
    MYSQL *msql = mysql_init(nullptr); 
    if (mysql_real_connect(msql, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0))
    {
        std::cerr << "MYSQL connect false" << std::endl;
        mysql_close(msql);
        return 1;
    }

    // 设置编码
    mysql_set_character_set(msql, "utf8");
    std::cout << "MYSQL connect success" << std::endl;

    // 操作数据库--sql语句
    std::string sql = "insert into student  values (2, \'wei2\')";
    int res = mysql_query(msql, sql.c_str());
    if (res != 0)
    {
        std::cout << "execute: " << sql << " failed" << std::endl;
        mysql_close(msql);
        return 2;
        
    }

    std::cout << "execute: " << sql << " success" << std::endl;

    mysql_close(msql);

    return 0;
}
